﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

using SdlDotNet.Graphics.Sprites;
using SdlDotNet.Graphics;
using System.IO;

using System.Drawing;

using SdlDotNet.Input;
namespace bloodbox
{
    class BBConsole
    {
        Surface BBScreen;
        string InputLine = " ";

        const int MaxMessages = 8;
        string[] LastMessages = new string[MaxMessages];
        Surface[] MsgSurfaces = new Surface[MaxMessages];
        
        public BBConsole(Surface screen)
        {
            BBScreen = screen;
            
            // initialization
            int i;
            for (i = 0; i < MaxMessages; i++)
            {
                LastMessages[i] = "";
            }

        }

        public void SendMessage()
        {
            int i;

            for (i = MaxMessages-1; i > 0; i--)
            {
                LastMessages[i] = LastMessages[i - 1];
            }
            LastMessages[0] = InputLine;

            InputLine = " ";
        }

        public void KeyPushed(string key, ModifierKeys mod)
        {
            if (key == "space")
            {
                InputLine += " ";
            }
            else if (key == "return")
            {
                SendMessage();
            }
            else if (key == "backspace")
            {
                InputLine = InputLine.Remove(InputLine.Length - 1);
            }
            else if (key == "tab")
            {
                InputLine += "    ";
                Console.WriteLine(InputLine);
            }
            // Avoid unhandled special caracters 
            else if (key.Length == 1)
            {
                if (mod == ModifierKeys.Caps || mod == ModifierKeys.ShiftKeys)
                    key.ToUpper();

                InputLine += key;
            }
        }

        public Surface getConsole()
        {
            Surface ConsoleSurface = new Surface(400, 105);

            for (int i = 0; i < MaxMessages; ++i)
            {
                MsgSurfaces[i] = TextDrawer.GetTextSurface(LastMessages[7-i], TextDrawer.TextFonts.Standard, Color.White);
                ConsoleSurface.Blit(MsgSurfaces[i], new Point(0, i*10));
            }

            ConsoleSurface.Blit(    TextDrawer.GetTextSurface("> "+getMainLine(), 
                                    TextDrawer.TextFonts.Standard, Color.White), new Point(0, 90)   );

            return ConsoleSurface;
        }

        public string getMainLine() {

            return InputLine;
        }

    }
}